package com.woniuxy.woniuhis.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.SurgeryBooking;
import com.woniuxy.woniuhis.entity.SurgeryRoom;
import com.woniuxy.woniuhis.service.SurgeryBookingService;
import com.woniuxy.woniuhis.service.SurgeryRoomService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@CrossOrigin("http://127.0.0.1:5500")
@RequestMapping("/surgeryBooking")
public class SurgeryBookingController {

	@Autowired
	SurgeryBookingService surgeryBookingService;

	@Autowired
	SurgeryRoomService surgeryRoomService;

	@RequestMapping("/add")
	@ResponseBody
	public Object addSurgeryBooking(@RequestBody SurgeryBooking surgeryBooking) {
		ResponseJson responseJson = new ResponseJson();
		try {
			System.out.println(surgeryBooking);
			surgeryBooking.setBookingId(UUIDUtil.uuid32());
			surgeryBooking.setBookingTime(new Date());
			surgeryBookingService.addSurgeryBooking(surgeryBooking);
			responseJson.setCode(0);
			responseJson.setMsg("添加成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}

	// 更新预约信息
	// 应该是只有未执行的情况下，才能修改预约信息，其他的情况不能修改
	@RequestMapping("/update")
	@ResponseBody
	public Object modifySurgeryBooking(@RequestBody SurgeryBooking surgeryBooking) {
		ResponseJson responseJson = new ResponseJson();
		// 需要判断修改的类型
		// 修改预约时间还是手术室
		try {
			if (StringUtils.isEmpty(surgeryBooking.getBookingId())) {
				throw new RuntimeException("预约单号为空");
			}
			SurgeryBooking temp = surgeryBookingService.findSurgeryBookingById(surgeryBooking.getBookingId());
			if (!"1".equals(temp.getSituation())) {
				throw new RuntimeException("只有未执行状态才能修改");
			}
			surgeryBookingService.modifySurgeryBooking(surgeryBooking);
			responseJson.setCode(0);
			responseJson.setMsg("修改成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}

	/*
	 * @RequestMapping("/delete")
	 * 
	 * @ResponseBody public Object deleteSurgeryBooking(@RequestBody SurgeryBooking
	 * surgeryBooking) { ResponseJson responseJson = new ResponseJson(); try {
	 * System.out.println(surgeryBooking); System.out.println(surgeryBooking);
	 * surgeryBookingService.modifySurgeryBooking(surgeryBooking);
	 * responseJson.setCode(0); responseJson.setMsg("修改成功"); return responseJson; }
	 * catch (Exception e) { responseJson.setCode(1);
	 * responseJson.setMsg(e.toString()); e.printStackTrace(); return responseJson;
	 * } }
	 */

	// 更改状态
	@RequestMapping("/updateStuation")
	@ResponseBody
	public ResponseJson updateStuation(@RequestBody SurgeryBooking surgeryBooking) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if (StringUtils.isEmpty(surgeryBooking.getBookingId())
					|| StringUtils.isEmpty(surgeryBooking.getSituation())) {
				throw new RuntimeException("不能为空");
			}
			surgeryBookingService.modifySurgeryBookingSituation(surgeryBooking);
			responseJson.setCode(0);
			responseJson.setMsg("修改状态成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}

	// 按手术室id查询，或者不设条件查询
	@RequestMapping("/all")
	@ResponseBody
	public Object getAllSurgeryBookings( @RequestBody  SurgeryBooking surgeryBooking) {
		ResponseJson<SurgeryBooking> responseJson = new ResponseJson<SurgeryBooking>();
		try {
			String surgeryRoomId = surgeryBooking.getSurgeryRoomId();
			if (surgeryRoomId == null || "".equals(surgeryRoomId)) {

			} else {
				SurgeryRoom surgeryRoom = surgeryRoomService.findSurgeryRoomById(surgeryRoomId);
				if (surgeryRoom == null) {
					throw new RuntimeException("手术室的编号不正确");
				}
				surgeryBooking.setSurgeryRoomId(surgeryRoomId);
			}
			Integer page = surgeryBooking.getPage();
			Integer limit = surgeryBooking.getLimit();
			if (page == null) {
				page = 1;
			}
			if (limit == null) {
				limit = 10;
			}

			PageInfo<SurgeryBooking> surgeryBookings = surgeryBookingService.findSurgeryBookings(surgeryBooking,page,limit);
			responseJson.setCode(0);
			// 总记录数
			responseJson.setCount((int)surgeryBookings.getTotal());
			responseJson.setMsg("");
			// 分页查询的条目
			responseJson.setData(surgeryBookings.getList());
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}

	}
}
